package com.dq.service.finance.impl;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.PayrollRecordQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.Vo;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.domain.org.Org;
import com.dq.domain.record.PayrollRecord;
import com.dq.repository.BaseRepositories;
import com.dq.service.finance.PayrollRecordService;
import com.dq.service.org.OrgService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author liuhui
 * @date 2019/11/28 0028 18:26:17
 */
@Service
public class PayrollRecordServiceImpl extends BaseRepositories implements PayrollRecordService {
    @Autowired
    private OrgService orgService;
    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public Vo findByWhere(PayrollRecordQuery payrollRecordQuery, Org org) throws AdminException {
        payrollRecordQuery.format();
        Map<String, Object> query = payrollRecordQuery.parseMap();
        if(payrollRecordQuery.getFlag() == 1){
            List<PayrollRecord.PayrollStatus> statuses = new ArrayList<>();
            statuses.add(PayrollRecord.PayrollStatus.waitConfirm);
            statuses.add(PayrollRecord.PayrollStatus.ConfirmSuccess);
            query.put("payrollStatus"+ BaseDaoImpl.inSqlStr,statuses);
            query.remove("payrollStatus");
        }
        if(StringUtils.isNotEmpty(payrollRecordQuery.getRealCName())){
            query.put("staffUser_userRealcName"+BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(payrollRecordQuery.getRealCName()));
            query.remove("realCName");
        }
        if(StringUtils.isNotEmpty(payrollRecordQuery.getCardNo())){
            query.put("staffUser_cardNo"+BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(payrollRecordQuery.getCardNo()));
            query.remove("cardNo");
        }
        if(null!=payrollRecordQuery.getBeginTime()){
            query.put("createTime"+BaseDaoImpl.gteSqlStr,payrollRecordQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(null!=payrollRecordQuery.getEndTime()){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,payrollRecordQuery.getEndTime());
            query.remove("endTime");
        }
        if(null!=payrollRecordQuery.getStaffUserId()){
            query.put("staffUser_id",payrollRecordQuery.getStaffUserId());
            query.remove("staffUserId");
        }
        if(null!=org){
            query.put("org_id",org.getId());
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            query.put("org" + BaseDaoImpl.inSqlStr, orgList);
        }
        query.remove("flag");
        List<PayrollRecord> list = dao.find(dao.query2Hql(PayrollRecord.class, query), query);
        Long count = dao.count(dao.count2Hql(PayrollRecord.class, query), query);
        List<PayrollRecord.OrgPayrollRecordListVo> payrollRecordListVos = new ArrayList<>();
        list.forEach(payrollRecord -> {payrollRecordListVos.add(payrollRecord.toOrgPayrollRecordListVo());});
        PageInfo pageInfo = new PageInfo(payrollRecordListVos);
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }

}
